package com.mofang.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mofang.MoFangApplication;
import com.mofang.R;

public class BaseTitleViewActiviey extends BaseActivity implements
		NavigationOnClickListener {

	private TextView titleView;
	private String navigationTitle, leftBtnText, rightBtnText;
	protected Button leftBtn, rightBtn;
	private Boolean navHidden = false;
	private LinearLayout navigationLayout;
	private ImageButton leftImgBtn, rightImgBtn;
	private int leftImageId;
	private int rightImageId;
	private ProgressDialog progressDialog;
	int progressDialogNum;

	@Override
	protected void onStart() {
		super.onStart();
		initCommonView();
	}

	public void hideNavigation(boolean hidden) {
		navHidden = hidden;
		if (null != navigationLayout) {
			if (navHidden) {
				navigationLayout.setVisibility(View.GONE);
			} else {
				navigationLayout.setVisibility(View.VISIBLE);
			}
		}
	}

	private void initCommonView() {
		navigationLayout = (LinearLayout) findViewById(R.id.navigationLayout);

		hideNavigation(navHidden);

		leftImgBtn = (ImageButton) findViewById(R.id.navigationLeftImageBtn);
		if (leftImgBtn != null) {
			leftImgBtn.setVisibility(View.INVISIBLE);
			leftImgBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					leftButtonOnClick();
				}
			});
		}
		rightImgBtn = (ImageButton) findViewById(R.id.navigationRightImageBtn);
		if (rightImgBtn != null) {
			rightImgBtn.setVisibility(View.INVISIBLE);
			rightImgBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					rightButtonOnClick();
				}
			});
		}
		titleView = (TextView) findViewById(R.id.navigation_title);
		leftBtn = (Button) findViewById(R.id.navigationLeftBtn);
		if (leftBtn != null) {
			leftBtn.setVisibility(View.GONE);
			leftBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					leftButtonOnClick();
				}
			});
		}

		rightBtn = (Button) findViewById(R.id.navigationRightBtn);
		if (rightBtn != null) {
			rightBtn.setVisibility(View.GONE);
			rightBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					rightButtonOnClick();
				}
			});
		}

		if (null != navigationTitle && null != titleView) {
			titleView.setText(navigationTitle);
		}

		if (leftImageId != 0 && null != leftImgBtn) {
			leftImgBtn.setImageResource(leftImageId);
			leftImgBtn.setVisibility(View.VISIBLE);
			leftBtn.setVisibility(View.GONE);
		}
		if (rightImageId != 0 && null != rightImgBtn) {
			rightImgBtn.setImageResource(rightImageId);
			rightImgBtn.setVisibility(View.VISIBLE);
			rightBtn.setVisibility(View.GONE);
		}
		if (null != leftBtnText && null != leftBtn) {
			leftBtn.setText(leftBtnText);
			leftBtn.setVisibility(View.VISIBLE);
			leftImgBtn.setVisibility(View.GONE);
		}
		if (null != rightBtnText && null != rightBtn) {
			rightBtn.setText(rightBtnText);
			rightImgBtn.setVisibility(View.GONE);
			rightBtn.setVisibility(View.VISIBLE);
		}
	}

	public void setNavigationTitle(String title) {
		navigationTitle = title;
		if (null != titleView) {
			titleView.setText(navigationTitle);
		}
	}

	public void setNavigationLeftBtnText(String text) {
		leftBtnText = text;
		if (null != leftBtn) {
			leftBtn.setVisibility(View.VISIBLE);
			leftImgBtn.setVisibility(View.GONE);
			leftBtn.setText(leftBtnText);
		}
	}

	public void setNavigationRightBtnText(String text) {
		rightBtnText = text;
		if (null != rightBtn) {
			rightBtn.setVisibility(View.VISIBLE);
			rightImgBtn.setVisibility(View.GONE);
			rightBtn.setText(rightBtnText);
		}
	}

	public void setNavigationLeftBtnImage(int imageId) {
		leftImageId = imageId;
		if (null != leftImgBtn) {
			leftImgBtn.setImageResource(leftImageId);
			leftImgBtn.setVisibility(View.VISIBLE);
			leftBtn.setVisibility(View.GONE);
		}
	}

	public void setNavigationRightBtnImage(int imageId) {
		rightImageId = imageId;
		if (null != rightImgBtn) {
			rightImgBtn.setImageResource(rightImageId);
			rightImgBtn.setVisibility(View.VISIBLE);
			rightBtn.setVisibility(View.GONE);
		}
	}

	public void goBack() {
		finish();
		// overridePendingTransition(R.anim.out_to_left, R.anim.in_from_right);

	}

	public void goBack(int requestCode) {
		finishActivity(requestCode);
		// overridePendingTransition(R.anim.out_to_left, R.anim.in_from_right);

	}

	public void gotoActivity(Intent intent) {
		startActivity(intent);
		// overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);

	}

	public void gotoActivity(Intent intent, int requestCode) {
		startActivityForResult(intent, requestCode);
		// overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);

	}

	public void setLeftNavigatoinOnClick() {

	}

	public void setNavigationTitle(int id) {
		setNavigationTitle(MoFangApplication.getAppContext().getResources()
				.getString(id));
	}

	@Override
	public void leftButtonOnClick() {
		if (leftBtnText != null || leftImageId != 0) {
			finish();
		}
	}

	@Override
	public void rightButtonOnClick() {

	}

	@Override
	public void onResume() {
		super.onResume();
		IntentFilter filter = new IntentFilter();
		filter.addAction("finish");
		getApplicationContext().registerReceiver(broadcastReceiver, filter);
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public boolean dispatchTouchEvent(android.view.MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			View v = getCurrentFocus();
			if (isShouldHideInput(v, event)) {
				hideSoftInput(v.getWindowToken());
			}
		}
		return super.dispatchTouchEvent(event);
	}

	@Override
	protected void onDestroy() {
		// 删除广播注册
		getApplicationContext().unregisterReceiver(broadcastReceiver);
		super.onDestroy();
	}

	private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			unregisterReceiver(this); // 这句话必须要写要不会报错，不写虽然能关闭，会报一堆错
			((Activity) context).finish();
		}
	};

	/**
	 * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时没必要隐藏
	 * 
	 * @param v
	 * @param event
	 * @return
	 */
	private boolean isShouldHideInput(View v, MotionEvent event) {
		if (v != null && (v instanceof EditText)) {
			int[] l = { 0, 0 };
			v.getLocationInWindow(l);
			int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
					+ v.getWidth();
			if (event.getX() > left && event.getX() < right
					&& event.getY() > top && event.getY() < bottom) {
				// 点击EditText的事件，忽略它。
				return false;
			} else {
				return true;
			}
		}
		// 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditView上，和用户用轨迹球选择其他的焦点
		return false;
	}

	private void hideSoftInput(IBinder token) {
		if (token != null) {
			InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			im.hideSoftInputFromWindow(token,
					InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}

	public int dip2px(float dpValue) {
		final float scale = this.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public int px2dip(float pxValue) {
		final float scale = this.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	private View progressDialogView;

	public void showMessageDialog(String title, String message) {
		if (progressDialog == null) {
			LayoutInflater inflater = LayoutInflater.from(this);
			progressDialogView = inflater.inflate(
					R.layout.common_dialog_layout, null);
			progressDialog = new ProgressDialog(this);
			progressDialog
					.setOnDismissListener(new DialogInterface.OnDismissListener() {

						@Override
						public void onDismiss(DialogInterface arg0) {
							// TODO Auto-generated method stub
							// progressDialogNum--;
							progressDialog = null;
						}
					});
		}
		progressDialogNum++;
		TextView titleView = (TextView) progressDialogView
				.findViewById(R.id.progressDialogTitle);
		if (title != null) {
			titleView.setText(title);
		} else {
			titleView.setVisibility(View.GONE);
		}
		TextView messageView = (TextView) progressDialogView
				.findViewById(R.id.progressDialogMessage);
		if (message != null) {
			messageView.setText(message);
		} else {
			messageView.setVisibility(View.GONE);
		}
		progressDialog.show();
		progressDialog.setContentView(progressDialogView);

	}

	public void closeMessageDialog() {
		if (progressDialog != null) {// && progressDialogNum==0
			progressDialog.dismiss();
			progressDialog = null;
		} else {
			progressDialogNum--;
		}
	}

	public void showToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	public void showToast(String message, int time) {
		Toast.makeText(this, message, time).show();
	}

}

interface NavigationOnClickListener {
	public void leftButtonOnClick();

	public void rightButtonOnClick();
}
